package com.superc.lib.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.superc.R;
import com.superc.lib.helper.KeyValue;
import com.superc.lib.ui.UIFunction;
import com.superc.model.adapter.IAdapaterViewCallback;
import com.superc.model.adapter.ListItemAdapter;
import com.superc.model.intent.MulitSelectorInfo;
import com.superc.model.intent.formatter.IFormatValue;
import com.superc.model.intent.formatter.IntervalFormater;

public class MulitSelector extends Activity {

	public static final String BUNDEL_NAME = "param";
	private MulitSelectorInfo selectionInfo = null;
	private ListItemAdapter<KeyValue<String,String>> adapter = null;
	private Activity my = null;
	private ListView lvwMSContent=null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mulitselectview);
		my=this;
		Bundle param = IntentFunction.getBundle(this);
		if (param != null) {
			selectionInfo = ((MulitSelectorInfo) param.getSerializable(BUNDEL_NAME));
			initUI();
		}
	}

	private void initUI() {
		adapter = new ListItemAdapter<KeyValue<String,String>>(this, R.layout.mulitselectviewitem, selectionInfo.datas,
				new IAdapaterViewCallback<KeyValue<String,String>>() {

					@Override
					public void OnCreateView(View parentView,
							KeyValue<String,String> item, int position,
							boolean bolFirstView) {
						TextView txtMSItem = UIFunction.getTextView(
								parentView, R.id.txtMSItem);
						try {
							IFormatValue formatter=null;
							if(selectionInfo.formatClassName.equals(IntervalFormater.class.getName())){
								formatter=new IntervalFormater(selectionInfo.unitName);
							}
							txtMSItem.setText(formatter.formatValue(item.getValue()));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					@Override
					public void OnViewClickListener(View v) {
						
					}
				});
		lvwMSContent=UIFunction.getListView(this, R.id.lvwMSContent);
		lvwMSContent.setAdapter(adapter);
		adapter.addSelectedFeature(lvwMSContent);
		UIFunction.getButton(my,R.id.cmdMSAdd).setOnClickListener(btnClick);
		UIFunction.getButton(my,R.id.cmdMSSave).setOnClickListener(btnClick);
		UIFunction.getButton(my,R.id.cmdMSDelete).setOnClickListener(btnClick);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode==RESULT_OK){
			@SuppressWarnings("unchecked")
			KeyValue<String,String> newItem=(KeyValue<String,String>)data.getExtras().getSerializable(MulitAddSelector.BUNDEL_NAME);
			selectionInfo.datas.add(newItem);
			initUI();
		}
	}

	private View.OnClickListener btnClick = new View.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.cmdMSSave:
					Intent resultIntent = new Intent();
					resultIntent.putExtra(MulitAddSelector.BUNDEL_NAME,selectionInfo);
					setResult(RESULT_OK, resultIntent);
					finish();
					break;
				case R.id.cmdMSAdd:{
					Intent addIntent=IntentFunction.getDirectIntent(my, selectionInfo.addClassInfo);
					startActivityForResult(addIntent,0);
					break;
				}
				case R.id.cmdMSDelete:{
					int index=adapter.getSelectedItemPosition();
					if(index!=-1){
						selectionInfo.datas.remove(index);
					}
					initUI();
					break;
				}
			}
		}
	};

	public static int[] getIntervalAlerts(MulitSelectorInfo selectionInfo) {
		int values[]=new int[selectionInfo.datas.size()];
		for (int i = 0; i < selectionInfo.datas.size(); i++) {
			values[i]= Integer.parseInt(selectionInfo.datas.get(i).getValue());
		}
		return values;
	}
	
}